package cl.ceis.demo6;

import android.app.Activity;
import android.content.Context;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class AudioRecActivity extends Activity {
    
	
	private static final String LOG_TAG = "AudioRecordTest";
    private static String mFileName = null;
    private RecordButton mRecordButton = null;
    private MediaRecorder mRecorder = null;
    private PlayButton   mPlayButton = null;
    private MediaPlayer   mPlayer = null;
    private String duracion = "";
    
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LinearLayout layout = new LinearLayout(this);
        TextView txt_duracion = new TextView(this);
        txt_duracion.setText("Duracion");
        mRecordButton = new RecordButton(this);
        layout.addView(mRecordButton, new LinearLayout.LayoutParams(
        		ViewGroup.LayoutParams.WRAP_CONTENT,
        		ViewGroup.LayoutParams.WRAP_CONTENT,
        		0));
        mPlayButton = new PlayButton(this);
        layout.addView(mPlayButton, new LinearLayout.LayoutParams(
        		ViewGroup.LayoutParams.WRAP_CONTENT,
        		ViewGroup.LayoutParams.WRAP_CONTENT,
        		0));
        layout.addView(txt_duracion);
        //setea el layout a la View
//        setContentView(R.layout.main);
        setContentView(layout);
        
      
        
    }
	
	@Override
    public void onPause() {
        super.onPause();
        if (mRecorder != null) {
            mRecorder.release();
            mRecorder = null;
        }

        if (mPlayer != null) {
            mPlayer.release();
            mPlayer = null;
        }
    }
	
    private void onRecord(boolean start) {
        if (start) {
            startRecording();
        } else {
            stopRecording();
        }
    }
    private void onPlay(boolean start){
        if (start) {
            startPlaying();
        } else {
            stopPlaying();
        }
    }
    
    /**
     * Detiende el player
     */
    private void stopPlaying() {
		mPlayer.release();
		mPlayer = null;
		
	}

    /**
     * Iniciar el player
     */
	private void startPlaying() {
	 mPlayer = new MediaPlayer();
		 try {
			 mPlayer.setDataSource(mFileName);
			 mPlayer.prepare();
			 mPlayer.start();
		
		 } catch (Exception e) {
			Log.e(LOG_TAG, "Error al inciar");
		 }
		
	}

	private void stopRecording() {
		mRecorder.stop();
		mRecorder.release();
		mRecorder = null;
		
	}


	private void startRecording() {
		mRecorder = new MediaRecorder();
		mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
		mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
		mRecorder.setOutputFile(mFileName);
		mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
		try {
			mRecorder.prepare();
		} catch (Exception e) {
			Log.e(LOG_TAG, "No se puede grabar");
		}
		mRecorder.start();
	}

	
	/**
	 * Clase para comenzar grabacion
	 * @author MariaJose
	 *
	 */
	class RecordButton extends Button{

		boolean mStartRecording = true;
		
		public RecordButton(Context context) {
			super(context);
			setText("Iniciando REC");
			setOnClickListener(clicker);
		}
		
		OnClickListener clicker =  new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				onRecord(mStartRecording);
				if(mStartRecording){
					setText("Deteniendo REC");
				}
				else{
					setText("Iniciando REC");
				}
				 mStartRecording = !mStartRecording;
			}
		};
		
		
	}
	/**
	 * Clase para reproducir
	 * @author MariaJose
	 *
	 */
	class PlayButton extends Button {
		
		boolean mStartPlaying = true;
		
        public PlayButton(Context context) {
			super(context);
			setText("Iniciando playing");
            setOnClickListener(clicker);
		}
        
        OnClickListener clicker = new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				onPlay(mStartPlaying);
				if (mStartPlaying) {
                    setText("Detener");
                } else {
                    setText("Start playing");
                }
                mStartPlaying = !mStartPlaying;
			}
		};

		
	}
	
	 public AudioRecActivity() {
	        mFileName = Environment.getExternalStorageDirectory().getAbsolutePath();
	        mFileName += "/audiorecordtest.mp3";
	    }
}